const express = require('express');
const router = express.Router();
const axios = require('../https');
const logger = require('../logger');

//login
router.post('/login',function(req,res,next){
    axios({
        method: 'post',
        url: '/borrow/user/login',
        data: req.body
    })
        .then(function(response){
            res.setHeader("P3P","CP=CAO PSA OUR");
            if(response.data.code=='200'){
                req.session.token = response.data.data.token;
                req.session.userPhone = req.body.username;
                req.session.updateTime = new Date().getTime();
                res.json({ "code": response.data.code, "success": response.data.success, "msg": "请求成功" });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/user/login 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error('接口请求报错：'+error+' from 接口：/borrow/user/login');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        });
});

//flowing
router.get('/flowing/list',function(req,res,next){
    axios({
        method: 'get',
        url: '/borrow/financier/account/flow/info',
        params: Object.assign(req.query, { token: req.session.token })
    })
        .then(function(response){
            if(response.data.code=='200'){
                res.json({ "code": response.data.code, "success": response.data.success, "msg": "请求成功", "financieraccountflowlist": response.data.data.financieraccountflowlist, "count": response.data.data.count });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/financier/account/flow/info 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error('接口请求报错：'+error+' from 接口： /borrow/financier/account/flow/info');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        })
});

//findPass
router.post('/findPassword',function(req,res,next){
    axios({
        method: 'post',
        url: '/borrow/user/smsg/password',
        data: req.body
    })
        .then(function(response){
            if(response.data.code == '200'){
                req.session.destroy();
                res.json({ "code": response.data.code, "success": response.data.success, "msg": "请求成功" });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/user/smsg/password 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error('接口请求报错：'+error+' from 接口： /borrow/user/smsg/password');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        })
});

//changePass
router.post('/changePasSword',function(req,res,next){
    axios({
        method: 'post',
        url: '/borrow/user/passwor/update',
        data: Object.assign(req.body, { token: req.session.token })
    })
        .then(function(response){
            if(response.data.code == '200'){
                req.session.destroy();
                res.json({ "code": response.data.code, "success": response.data.success, "msg": "请求成功" });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/user/passwor/update 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error('接口请求报错：'+error+' from 接口： /borrow/user/passwor/update');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        })
});

//sendMess
router.get('/sendPass',function(req,res,next){
    axios({
        method: 'get',
        url: '/borrow/user/smsg',
        params: req.query
    })
        .then(function(response){
            if(response.data.code == '200'){
                res.json({ "code": response.data.code, "success": response.data.success, "msg": "请求成功" });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/user/smsg 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error('接口请求报错：'+error+' from 接口： /borrow/user/smsg');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        })
});

//loginOut
router.post('/loginOut',function(req,res,next){
    axios({
        method: 'post',
        url: '/borrow/user/logout',
        data: {
            token: req.session.token
        }
    })
        .then(function(response){
            if(response.data.code == '200'){
                req.session.destroy();
                res.json({ "code": response.data.code, "success": response.data.success, "msg": '请求成功' });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/user/logout 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error(error+' from 接口： /borrow/user/logout');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        })
});

//accountList
router.get('/accountList',function(req,res,next){
    axios({
        method: 'get',
        url: '/borrow/financier/account/info',
        params: Object.assign(req.query, { token: req.session.token })
    })
        .then(function(response){
            if(response.data.code=='200'){
                res.json({ "code": response.data.code, "success": response.data.success, "msg": "请求成功", "borrowAccountList": response.data.data.borrowAccountList, "count": response.data.data.count });
            }else{
                res.json({ "code": response.data.code, "success": response.data.success, "msg": response.data.cause });
            }
        })
        .catch(function(error){
            if(!req.session) {
                logger.error('req.session is' + req.session);
            }
            if(error.response){
                logger.info('接口请求报错：接口： /borrow/financier/account/info 存在请求，但是服务器返回一个状态吗，在2xx之外');
                res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
            }else{
                logger.error('接口请求报错：'+error+' from 接口： /borrow/financier/account/info');
                res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
            }
        })
});

//imgcode
router.get('/getimg',function (req, res, next) {
    axios({
        method: 'get',
        url: '/borrow/comm/captcha',
        params:{
            width: 115,
            height: 36
        },
        responseType:'arraybuffer'
    }).then(function(response){
        var buff = new Buffer(response.data,'binary');
        res.writeHead(200,{'content-type':"image/png"});
        res.end(buff);
    }).catch(function(error){
        if(!req.session) {
            logger.error('req.session is' + req.session);
        }
        if(error.response){
            logger.info('接口请求报错：接口： /borrow/comm/captcha 存在请求，但是服务器返回一个状态吗，在2xx之外');
            res.json({ "code": error.response.data.code,"success": error.response.data.success, "msg": error.response.data.cause });
        }else{
            logger.error('接口请求报错：'+error+' from 接口： /borrow/comm/captcha');
            res.json({ "code": "is.error","success": false, "msg": "请求接口失败" });
        }
    });
});

module.exports = router;